var app = getApp()
Page({
  data: {
    isShowturn:false,
    detailsType:1,
    availincome:'',
    toPhone: "",
    page: 0,
    noMoretip: false,
    showLoading: true,
    sqkdate:"",
    sqjdate:"",
    meatmoney:"0.00",
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.title) {
      app.setNavtitle(e.title);
      that.setData({
        title: e.title
      })
    } else {
      app.setNavtitle('小金库');
    }
    app.setVersion(that);
  },
  onShow: function () {
    var that = this;
    that.onPullDownRefresh();
  },
  requestDetails: function () {
    var value = wx.getStorageSync('shoptoken');

    var that = this;
    var data = {};
    var page = that.data.page;
    data.shoptoken = value.token;
    data.os_id = value.os_id;
    data.map = 'applet_meat_pig_used_history';
    data.page = page;
    data.type = that.data.detailsType;
    data.status = that.data.couponStatus;
    data.sqkdate = that.data.sqkdate;
    data.sqjdate = that.data.sqjdate;
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.detailsList;
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            detailsList: allArr,
            meatmoney:res.data.statistic.totalamount
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.detailsList);
        } else {
          that.setData({
            meatmoney: "0.00",
          });
          if (page <= 0) {
            that.setData({
              detailsList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false,
            });
          }
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        that.setData({
          showLoading: false
        })
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      detailsType: 1
    });
    // that.requestMoney();
    that.requestDetails();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestDetails();
    }
  },
  // 转到余额金额
  availincomeChange:function(e){
    this.setData({
      availincome:e.detail.value
    })
  },
  availPhoneChange:function(e){
    this.setData({
      toPhone:e.detail.value
    })
  },
  // 全部转入
  allTurn:function(){
    this.setData({
      availincome: this.data.moneyinfo.deduct_ktx
    })
  },
  toggleDetailstype:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      detailsType:type,
      detailsList:null,
      page: 0,
      noMoretip: false,
      showLoading: true
    })
    that.requestDetails();
  },
  // 提现
  redbagWithdraw: function () {
    wx.navigateTo({
      url: '/subpages/incomeWithdraw/incomeWithdraw'
    })
  },
  // 提现规则
  withdrawRules: function () {
    wx.navigateTo({
      url: '/subpages/withdrawRules/withdrawRules'
    })
  },
  // 充值
  walletRecharge: function () {
    wx.navigateTo({
      url: '/subpages/walletRecharge/walletRecharge'
    })
  },
  // 禁止滑动页面
  catchtouchstart:function(){
    console.log("触摸屏幕阻止冒泡");
  },
  showTurnmodal:function(){
    this.setData({
      isShowturn:true
    })
  },
  hideTurnmodal: function () {
    this.setData({
      isShowturn: false
    })
  },
  //核销二维码显示
  codeModelShow: function () {
    var that = this;
    that.requestCode('showCode');
  },
  //核销二维码弹窗关闭
  closeCodeModel: function () {
    var that = this;
    that.setData({
      codeShow: false
    })
  },
  // 显示充值类型弹窗
  showRechargetype: function () {
    var that = this;
    that.setData({
      isRechargetype: true
    })
  },
  // 隐藏充值类型弹窗
  hideRechargetype:function(){
    var that = this;
    that.setData({
      isRechargetype: false
    })
  },
  confirmRechagretype:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.hideRechargetype();
    wx.navigateTo({
      url: '/subpages/walletRecharge/walletRecharge?type='+type
    })
  },
  bindDateChangesqk(e){
    this.setData({
      sqkdate: e.detail.value?e.detail.value:''
    })
    let that = this;
    that.setData({
      orderList: null,
      orderfenlei: '',
      page: 0,
      noMoretip: false,
      showLoading: true,
      sqkdate:that.data.sqkdate,
      sqjdate:that.data.sqjdate,
    })
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      detailsType: 1
    });
    that.requestDetails();
  },
  bindDateChangesqj(e){

    this.setData({
      sqjdate: e.detail.value?e.detail.value:''
    })
    let that = this;
    that.setData({
      orderList: null,
      orderfenlei: '',
      page: 0,
      noMoretip: false,
      showLoading: true,
      sqkdate:that.data.sqkdate,
      sqjdate:that.data.sqjdate,
    })
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      detailsType: 1
    });
    that.requestDetails();
  },
})